Skip to content

Keep TopoJson styles out of the data passed in - #2380

Open
mmporong wants to merge 1 commit into
python-visualization:mainfrom
mmporong:fix-topojson-style-shared-data
Open

mmporong wants to merge 1 commit into
python-visualization:mainfrom
mmporong:fix-topojson-style-shared-data

Conversation

@mmporong

Copy link
Copy Markdown

TopoJson.style_data() writes each feature's style into the dict that was passed to TopoJson. When two layers are made from the same dict, the second layer is rendered with the first layer's style merged into its own, and a layer without a style_function picks up the styles of the layers before it. The dict the caller passed in also comes back with properties.style added to its geometries.

import folium

topo = {
    "type": "Topology",
    "objects": {
        "shapes": {
            "type": "GeometryCollection",
            "geometries": [
                {"type": "Polygon", "arcs": [[0]], "properties": {"name": "A"}}
            ],
        }
    },
    "arcs": [[[0, 0], [1, 0], [0, 1], [-1, -1]]],
    "transform": {"scale": [1, 1], "translate": [0, 0]},
}

m = folium.Map()
red = folium.TopoJson(
    topo, "objects.shapes", style_function=lambda f: {"fillColor": "red"}
).add_to(m)
blue = folium.TopoJson(
    topo, "objects.shapes", style_function=lambda f: {"color": "blue"}
).add_to(m)
m.get_root().render()

print(blue.data["objects"]["shapes"]["geometries"][0]["properties"]["style"])
# main:    {'fillColor': 'red', 'color': 'blue'}
# this PR: {'color': 'blue'}
print(topo["objects"]["shapes"]["geometries"][0]["properties"])
# main:    {'name': 'A', 'style': {'fillColor': 'red', 'color': 'blue'}}
# this PR: {'name': 'A'}

style_data() now writes the styles into copies of the dicts along the object path, of the geometries and of their properties, and self.data points at that copy. Styles already present in the input data are kept and merged as before. The arcs and the rest of the topology are shared with the input rather than copied: a copy.deepcopy of the whole topology took the render of examples/data/us_counties_20m_topo.json from about 115 ms to about 380 ms, while with this change style_data() on that file (3221 geometries) takes about 1.7 ms instead of 0.8 ms (median of 200 runs).

Inputs that already raised on main (an object path that does not exist, a geometry with "properties": null) still raise, with a different exception type.

No linked issue; this was found by reading TopoJson.style_data().

Testing

Python 3.12, numpy 2.5.5, jinja2 3.1.6, branca 0.8.2.

  • The two existing tests test_topojson_non_collection_geometry and test_topojson_non_collection_geometry_without_type checked the style on the dict passed in, which is the behavior this changes, so they now check topojson.data.
  • Added test_topojson_style_does_not_change_input_data, which builds two layers from one dict and checks each layer's style and that the dict is unchanged. It fails on main and passes with this change.
  • A script over single layers (GeometryCollection with and without properties, pre-styled properties, the default style_function, a single geometry, a nested object path, an empty collection, and a Choropleth with topojson= on tests/or_counties_topo.json) gives the same rendered data on main and on this branch.
  • pytest tests --ignore=tests/selenium --ignore=tests/snapshots --ignore=tests/test_repr.py: 426 passed with this change, 425 on main. The same 2 tests fail on both because my environment has no network access and no geodatasets (test_json_request, test_timedynamic_geo_json).
  • black 26.10.1, ruff 0.17.5 and codespell 2.4.3 (the pre-commit versions) pass on the changed files.

TopoJson.style_data() wrote each feature's style into the dict given to
TopoJson, so a second layer made from the same dict rendered the first
layer's style as well. Write the styles into copies of the object path,
the geometries and their properties instead, leaving the arcs shared.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant